﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using ICSharpCode.SharpZipLib.Zip;

namespace ipaidabribe.Code
{
    public class ClsZipFile
    {
        public List<string> lstFileName;

        public bool zipFiles(string strZipFileName)
        {
            bool booIsOk = false;
            try
            {
                if (lstFileName != null)
                {
                    ZipFile zipFile = ZipFile.Create(strZipFileName);
                    zipFile.BeginUpdate();
                    foreach (string strSourceFile in lstFileName)
                    {
                        zipFile.Add(strSourceFile);
                    }
                    zipFile.CommitUpdate();
                    zipFile.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return booIsOk;
        }

        public void extractZipFiles(string zipFileName)
        {
            string strDirectoryName = Path.GetDirectoryName(zipFileName) + @"\" + Path.GetFileNameWithoutExtension(zipFileName);
            ZipInputStream zis = new ZipInputStream(File.Open(zipFileName, FileMode.Open));
            if (!Directory.Exists(strDirectoryName))
            {
                Directory.CreateDirectory(strDirectoryName);
            }

            ZipEntry ze = zis.GetNextEntry();
            while (ze != null)
            {
                FileStream fs = File.Create(strDirectoryName + @"\" + Path.GetFileName(ze.Name));
                int len = 1024;
                byte[] bytes = new byte[len];
                while (0 < (len = zis.Read(bytes, 0, len)))
                {
                    fs.Write(bytes, 0, len);
                }
                fs.Close();
                ze = zis.GetNextEntry();
            }
            zis.Close();
        }
    }
}